package com.ruoyi.order.listener;

import com.ruoyi.order.domain.MesProductBarcode;
import com.ruoyi.order.domain.MesTopOrder;
import com.ruoyi.order.enums.QRCodeStatusEnum;
import com.ruoyi.order.event.ProductBarcodeDataEvent;
import com.ruoyi.order.service.IMesProductBarcodeService;
import com.ruoyi.order.service.IMesTopOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ProductBarcodeListener {

    @Autowired
    private IMesProductBarcodeService productBarcodeService;
    @Autowired
    private IMesTopOrderService orderService;

    @EventListener
    public void insertProductBarcode(ProductBarcodeDataEvent event) {
        MesTopOrder order = orderService.selectMesTopOrderProducting();
        event.getProcessBarcodeList().forEach(productBarcode -> {
            if (StringUtils.isNotBlank(productBarcode.getBarcode())) {
                log.info("产品码数据：{}", productBarcode);
                MesProductBarcode mesProductBarcode = new MesProductBarcode();
                mesProductBarcode.setPartId(productBarcode.getPartNumber());
                mesProductBarcode.setOrderNumber(order.getOrderNumber());
                mesProductBarcode.setModelCode(productBarcode.getModelCode());
                mesProductBarcode.setQrCode(productBarcode.getBarcode());
                mesProductBarcode.setModelCode(productBarcode.getModelCode());
                mesProductBarcode.setQrCodeType(productBarcode.getBarcodeType());
                mesProductBarcode.setStationCode(productBarcode.getStationCode());
                mesProductBarcode.setStatus(QRCodeStatusEnum.VALID.getCode());
                productBarcodeService.insertMesProductBarcode(mesProductBarcode);
            }
        });
    }
}
